using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;


namespace DCApp
{
    public partial class ResultsDlg : Form
    {

        int _totalParticpants;
        List<DCHelper.ParticipantConnectionDetails> _partipantConnectionDetailsList = null;
        DateTime _dtSince;
        XmlDocument _downloadedXmlDoc = null;

        public ResultsDlg(int totalParticipants, DateTime dtSince, List<DCHelper.ParticipantConnectionDetails> partipantConnectionDetailsList)
        {
            InitializeComponent();
            _totalParticpants = totalParticipants;
            _partipantConnectionDetailsList = partipantConnectionDetailsList;
            _dtSince = dtSince;

        }

        private void Form_Load(object sender, EventArgs e)
        {
            _downloadedXmlDoc = DCHelper.Functions.GetParticipantData(_dtSince, _partipantConnectionDetailsList);


            string filterTemplate = "{0} or {1} participants\r\nRecords since {2}";
            lblDateRetrieved.Text = DateTime.Now.ToString();
            string filterString = String.Format(filterTemplate,_partipantConnectionDetailsList.Count,_totalParticpants.ToString(),_dtSince.Date.ToShortDateString());
            lblFilters.Text = filterString;

            //Save the results to a temp file and point the
            //web browser control to that so that it displays xml
            //neatly formatted 
                        
            string tempFile = System.IO.Path.GetTempFileName();
            tempFile += ".xml";
            _downloadedXmlDoc.Save(tempFile);
            webBrowserResults.Navigate(tempFile);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDlg = new SaveFileDialog();

            saveFileDlg.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
            saveFileDlg.FilterIndex = 1;
            saveFileDlg.RestoreDirectory = true;

            if (saveFileDlg.ShowDialog() == DialogResult.OK)
            {
                _downloadedXmlDoc.Save(saveFileDlg.FileName);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();    
        }

    }
}